from PySide6.QtWidgets import QFrame, QHBoxLayout, QStackedWidget
from qfluentwidgets import FlowLayout

from components.card import AppCard
from resource import OfficeIcon
from .word_view import WordToPdfFrame, DocAndDocxFrame, WordMergeFrame, ImgExtractFrame, WordReplaceFrame


class WordHomeFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = FlowLayout(self, needAni=True)

        self.to_pdf_card = AppCard(OfficeIcon.PDF2, 'To PDF', 'Word 批量转成 PDF')
        self.docx_and_doc_card = AppCard(OfficeIcon.WORD2, 'Doc & Docx', 'doc格式 & docx格式 批量互转')
        self.word_merge = AppCard(OfficeIcon.WORD3, 'Word Merge', 'Word 批量合并')
        self.img_extract = AppCard(OfficeIcon.IMG, 'Image Extract', '提取 Word 中的图片')
        self.replace = AppCard(OfficeIcon.DATA1, 'Word Replace', '替换Word中的数据信息，并保存')

        self.layout.addWidget(self.to_pdf_card)
        self.layout.addWidget(self.docx_and_doc_card)
        self.layout.addWidget(self.word_merge)
        self.layout.addWidget(self.img_extract)
        self.layout.addWidget(self.replace)


class WordInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('WordInterface')

        self.stackWidget = QStackedWidget(self)

        self.initLayout()
        self.initStackedWidget()
        self.initButtonEvent()

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 40, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def initStackedWidget(self):
        self.homeFrame = WordHomeFrame()
        self.wordToPdfFrame = WordToPdfFrame()
        self.docAndDocxFrame = DocAndDocxFrame()
        self.wordMergeFrame = WordMergeFrame()
        self.imgExtractFrame = ImgExtractFrame()
        self.replaceFrame = WordReplaceFrame()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.wordToPdfFrame)
        self.stackWidget.addWidget(self.docAndDocxFrame)
        self.stackWidget.addWidget(self.wordMergeFrame)
        self.stackWidget.addWidget(self.imgExtractFrame)
        self.stackWidget.addWidget(self.replaceFrame)

        self.stackWidget.setCurrentIndex(0)

    def initButtonEvent(self):
        # 首页切换
        self.homeFrame.to_pdf_card.clicked.connect(lambda: self.switchTo(self.wordToPdfFrame))
        self.homeFrame.docx_and_doc_card.clicked.connect(lambda: self.switchTo(self.docAndDocxFrame))
        self.homeFrame.word_merge.clicked.connect(lambda: self.switchTo(self.wordMergeFrame))
        self.homeFrame.img_extract.clicked.connect(lambda: self.switchTo(self.imgExtractFrame))
        self.homeFrame.replace.clicked.connect(lambda: self.switchTo(self.replaceFrame))
        # 返回按钮事件
        self.wordToPdfFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.docAndDocxFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.wordMergeFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.imgExtractFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.replaceFrame.returned.connect(lambda: self.switchTo(self.homeFrame))

    def switchTo(self, widget):
        self.stackWidget.setCurrentWidget(widget)
